package cn.dnui_wwt524.dnuifood_16110100524.controller;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cn.dnui_wwt524.dnuifood_16110100524.R;
import cn.dnui_wwt524.dnuifood_16110100524.bean.ResultBean;
import cn.dnui_wwt524.dnuifood_16110100524.listener.OnRetrofitListener;
import cn.dnui_wwt524.dnuifood_16110100524.model.UserModel;

public class ResultActivity extends BaseActivity
implements OnRetrofitListener<ResultBean> {
    private EditText res_user,res_password,res_repassword,res_mobile,res_add,res_comment;
    private Button res_zhuce;

    @Override
    void initViews() {
        layout_file =R.layout.activity_result;
        setLayout(layout_file);
        res_user = findViewById(R.id.res_user);
        res_password = findViewById(R.id.res_pass);
        res_repassword = findViewById(R.id.res_passok);
        res_mobile = findViewById(R.id.res_mobile);
        res_add = findViewById(R.id.res_add);
        res_comment = findViewById(R.id.res_comment);
        res_zhuce = findViewById(R.id.res_button);


    }

    @Override
    void initEvents() {
        res_zhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (res_user.getText().toString().trim().equals("")||res_password.getText().toString().trim().equals("")){
                    Toast.makeText(ResultActivity.this,"用户名和密码不能为空",Toast.LENGTH_SHORT).show();
                    if (res_password.getText().toString().trim().equals(res_repassword.getText().toString().trim())){
                        Toast.makeText(ResultActivity.this,"两次密码输入不对",Toast.LENGTH_SHORT).show();
                        if (res_mobile.getText().toString().trim().equals("")){
                            Toast.makeText(ResultActivity.this,"请输入手机号",Toast.LENGTH_SHORT).show();
                            if (res_add.getText().toString().trim().equals("")){
                                Toast.makeText(ResultActivity.this,"请输入地址",Toast.LENGTH_SHORT).show();
                                if (res_add.getText().toString().trim().equals("")) {
                                    Toast.makeText(ResultActivity.this, "请输入地址", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    }
                }
                else
                    initDate();
            }
        });

    }

    @Override
    void initDate() {
        UserModel userModel = new UserModel();
        userModel.reg(res_user.getText().toString().trim(),res_repassword.getText().toString().trim(),res_mobile.getText().toString().trim(),res_add.getText().toString().trim(),res_comment.getText().toString().trim(),this);



    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        initViews();
        initEvents();
    }

    @Override
    public void onSuccess(ResultBean object,int flag) {
        if (object.getSuccess().equals("1")){
            Toast.makeText(ResultActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
            Intent intent  = new Intent(ResultActivity.this,LoginActivity.class);
            startActivity(intent);
        }
        else{
            Toast.makeText(ResultActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(ResultActivity.this,msg,Toast.LENGTH_SHORT).show();

    }
}
